package com.ruoyi.api.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.api.dto.BizPhoneLocationDTO;
import com.ruoyi.api.dto.LocationDTO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.api.domain.BizPhoneLocation;
import com.ruoyi.api.service.IBizPhoneLocationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 电话位置Controller
 * 
 * @author 魏建波
 * @date 2022-01-21
 */
@RestController
@RequestMapping("/api2/location")
public class BizPhoneLocationController extends BaseController
{
    @Autowired
    private IBizPhoneLocationService bizPhoneLocationService;

    /**
     * 查询电话位置列表
     */
    @PreAuthorize("@ss.hasPermi('api:location:list')")
    @GetMapping("/list")
    public AjaxResult list(BizPhoneLocation bizPhoneLocation)
    {
        AjaxResult ajaxResult = AjaxResult.success("获取电话位置列表");
        List<BizPhoneLocation> list = bizPhoneLocationService.selectBizPhoneLocationList(bizPhoneLocation);
        // 组装一个带坐标的数据 返回到前端页面
        /*List<BizPhoneLocationDTO> list2 = new ArrayList<>();
        for(BizPhoneLocation phoneLocation:list){
            String[] data = new String[2];
            data[0] = phoneLocation.getLongitude();
            data[1] = phoneLocation.getLatitude();
            BizPhoneLocationDTO bizPhoneLocationDTO = new BizPhoneLocationDTO();
           *//* LocationDTO locationDTO = new LocationDTO();
            locationDTO.setLat(phoneLocation.getLatitude());
            locationDTO.setLng(phoneLocation.getLongitude());*//*
            bizPhoneLocationDTO.setPosition(data);
            list2.add(bizPhoneLocationDTO);
        }*/
        ajaxResult.put("data",list);
        ajaxResult.put("total",list.size());
        // 获得最后一个点的位置信息
        BizPhoneLocation lastLocation = bizPhoneLocationService.selectLastBizPhoneLocation(bizPhoneLocation);
        ajaxResult.put("lastLocation",lastLocation);
        return ajaxResult;
    }

    /**
     * 导出电话位置列表
     */
    @PreAuthorize("@ss.hasPermi('api:location:export')")
    @Log(title = "电话位置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizPhoneLocation bizPhoneLocation)
    {
        List<BizPhoneLocation> list = bizPhoneLocationService.selectBizPhoneLocationList(bizPhoneLocation);
        ExcelUtil<BizPhoneLocation> util = new ExcelUtil<BizPhoneLocation>(BizPhoneLocation.class);
        util.exportExcel(response, list, "电话位置数据");
    }

    /**
     * 获取电话位置详细信息
     */
    @PreAuthorize("@ss.hasPermi('api:location:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bizPhoneLocationService.selectBizPhoneLocationById(id));
    }

    /**
     * 新增电话位置
     */
    @PostMapping
    public AjaxResult add(@RequestBody BizPhoneLocation bizPhoneLocation)
    {
        return toAjax(bizPhoneLocationService.insertBizPhoneLocation(bizPhoneLocation));
    }

    /**
     * 修改电话位置
     */
    @PreAuthorize("@ss.hasPermi('api:location:edit')")
    @Log(title = "电话位置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizPhoneLocation bizPhoneLocation)
    {
        return toAjax(bizPhoneLocationService.updateBizPhoneLocation(bizPhoneLocation));
    }

    /**
     * 删除电话位置
     */
    @PreAuthorize("@ss.hasPermi('api:location:remove')")
    @Log(title = "电话位置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizPhoneLocationService.deleteBizPhoneLocationByIds(ids));
    }

    /**
     * 清空电话位置
     * @return
     */
    @Log(title = "清空电话位置", businessType = BusinessType.CLEAN)
    @PreAuthorize("@ss.hasPermi('api:location:remove')")
    @DeleteMapping("/clean")
    public AjaxResult clean()
    {
        bizPhoneLocationService.cleanOperLocation();
        return AjaxResult.success();
    }
}
